package application;
/**
 * this class is the utility class
 * which implements the function to display your files in specific folders
 * @author FlowerYang
 * @version 1.2.0
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.lang.*;

import base.*;

public class Application {
    private static  BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    private static PrintWriter stdOut = new PrintWriter(System.out, true);
    private static PrintWriter stdErr = new PrintWriter(System.err, true);
    public static void main(String[] args)throws IOException{
        Application App=new Application();
        App.run();
    }
    /**
     * Get the choices from Users.
     */
    private int GetChoice()throws IOException {
        int choice;
        while (true) {
            stdOut.printf("====================\n" +
                    "[0].  EXIT\n"+
                    "[1].  DISPLAY YOUR FILES\n"+
                    "choice>");
            try {
                choice = Integer.parseInt(stdIn.readLine());
                if (choice >= 0 && choice <= 1) return choice;
                else stdOut.println("Invalid Input\n");
            } catch (NumberFormatException nfe) {
                stdErr.println(nfe);
                stdErr.flush();
            }
        }
    }

    /**
     * Input user's specific path
     * @throws IOException
     */
    public void run()throws IOException{
        stdOut.printf("Welcome!\n");
        while (true){
            int choice=GetChoice();
            if (choice==0)break;
            stdOut.printf("Your Path Now Is : %s\n",System.getProperty("user.dir"));
            stdOut.println("Please Input The File Path You Want To Check:");
            String path=stdIn.readLine();
            work(path);
        }
    }
    public static void work(String path){
        stdOut.printf("You Want To Check:%s\n",path);

        /** Get the folder name */
        StringTokenizer tokenizer = new StringTokenizer(path, "\\");
        String folderName = tokenizer.nextToken();
        for (; tokenizer.hasMoreTokens();) {
            folderName = tokenizer.nextToken();
        }

        /** Get all file items <code>files<code> of the current user path */
        java.io.File file = new java.io.File(path);

        /** Get all file items <code>files<code> of specified path */

        java.io.File[] files = file.listFiles();

        if (files != null) {

            /**
             * Recursive printout all folder items of the current folder by
             * using java.io.File
             *
             */
            stdOut.println(folderName);
            printFolderItems(file, "--");

            stdOut.println("====Up:java.io.File========Down:filesystem===========");

            /**
             * Recursive construct <code>folder<code> include all folder items
             * by using <code>File<code>, <code>Folder<code>, <code>FolderItem<code>
             * and java.io.File
             *
             */
            Folder folder = new Folder(folderName);
            for (java.io.File f : files) {
                FolderItem folderItem;
                if (f.isFile()) {
                    folderItem = new File(f.getName(), "  ", (int) f.length());
                    folder.addFolderItem(folderItem);
                }
                if (f.isDirectory()) {
                    folderItem = new Folder(f.getName());
                    folder.addFolderItem(folderItem);
                    constructFolderItems(f, (Folder) folderItem);
                }
            }

            /**
             * Recursive printout all folder items of the current folder by
             * using <code>File<code>, <code>Folder<code> and <code>FolderItem<code>
             *
             */
            stdOut.println(folder.getName());
            printFolderItems(folder, "--");
        }
    }
    public static void constructFolderItems(java.io.File f, Folder folder) {
        java.io.File[] subFiles = f.listFiles();
        for (java.io.File i : subFiles) {
            FolderItem folderItem;
            if (i.isFile()) {
                folderItem = new File(i.getName(), "  ", (int) i.length());
                folder.addFolderItem(folderItem);
            }
            if (i.isDirectory()) {
                folderItem = new Folder(i.getName());
                folder.addFolderItem(folderItem);
                constructFolderItems(i, (Folder) folderItem);
            }
        }
    }
    public static void printFolderItems(java.io.File f, String indent) {
        for (java.io.File i : f.listFiles()) {
            stdOut.println(indent + i.getName());
            if (i.isDirectory()) {
                String indentTemp = indent + "--";
                printFolderItems(i, indentTemp);
            }
        }
    }
    public static void printFolderItems(Folder f, String indent) {
        for (FolderItem item : f.getFolderItems()) {
            stdOut.println(indent + item.getName());
            if (item instanceof Folder) {
                String indentTemp = indent + "--";
                printFolderItems((Folder) item, indentTemp);
            }
        }
    }
}
